package com.lm56.web.queryhistory;

import com.lm56.common.MyRequestBody;
import com.lm56.web.security.SecUserDetails;
import com.lm56.web.waybill.domain.WaybillTrace;
import com.lm56.web.waybill.domain.WaybillTraceDTO2;
import com.lm56.web.waybill.mapper.WaybillMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.management.Query;
import java.util.List;

/**
 * Created by Kelvin Yeung on 2016/12/10.
 */
@Component
@Transactional
public class QueryHistoryService {

    public static final int TYPE_EXPRESS = 1;
    public static final int TYPE_LOGISTICS = 2;

    @Autowired
    QueryHistoryMapper queryHistoryMapper;
    @Autowired
    WaybillMapper waybillMapper;

    public void log(QueryHistory queryHistory) {
        SecUserDetails secUser = (SecUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        queryHistory.setUserId(secUser.getUserId());
        queryHistoryMapper.insertQueryHistory(queryHistory);
    }


    public void log(Long waybillId,String waybillNo, String comName,Integer queryType) {
        try {
            QueryHistory queryHistory = new QueryHistory();
            SecUserDetails secUser = (SecUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            queryHistory.setUserId(secUser.getUserId());
            queryHistory.setWaybillId(waybillId);
            queryHistory.setComName(comName);
            queryHistory.setWaybillNo(waybillNo);
            queryHistory.setQueryType(queryType);
            queryHistory.setQueryType(QueryHistory.TYPE_LOGISTICS);
            queryHistoryMapper.insertQueryHistory(queryHistory);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void log(String waybillNo, String comName, Integer queryType) {

        try {
            QueryHistory queryHistory = new QueryHistory();
            SecUserDetails secUser = (SecUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            queryHistory.setUserId(secUser.getUserId());
            queryHistory.setComName(comName);
            queryHistory.setWaybillNo(waybillNo);
            queryHistory.setQueryType(queryType);
            queryHistoryMapper.insertQueryHistory(queryHistory);
        } catch (Exception e) {
           e.printStackTrace();
        }
    }

    public List<QueryHistory> getQueryHistory(MyRequestBody<QueryHistoryQueryForm> requestBody) {
        SecUserDetails secUser = (SecUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        requestBody.getParams().setUserId(secUser.getUserId());
        return queryHistoryMapper.getQueryHistories(requestBody);
    }

    public Integer getQueryHistoriesCount(MyRequestBody<QueryHistoryQueryForm> requestBody) {
        SecUserDetails secUser = (SecUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        requestBody.getParams().setUserId(secUser.getUserId());
        return queryHistoryMapper.getQueryHistoriesCount(requestBody);
    }
    public void deleteQueryHistory(Long id) {
        SecUserDetails secUser = (SecUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        queryHistoryMapper.deleteQueryHistory(id, secUser.getUserId());
    }


}
